package com.tuanzi.controller.admin;

import com.tuanzi.ResultVo.ResultVO;
import com.tuanzi.service.LoginService;
import com.tuanzi.utils.Status;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author tuanzi
 * @version 1.0
 * @date 2022/3/30 14:19
 */

@RestController
@RequestMapping("/admin")
public class LoginController {

    // 这里定死一个code验证码，用来检验是否有资格登录
    private final String finalCode = "1";

    @Resource
    private LoginService loginService;

    // 采用Restful风格书写

    /**
     *
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @return 正确用户的所有信息
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultVO login(@RequestParam String username, @RequestParam String password
            , @RequestParam String code, HttpSession session){
        ResultVO vo;
        if (finalCode.equals(code)){
            vo = loginService.login(username, password);
            session.setAttribute("user", vo.getContent());
        }else {
            vo = new ResultVO(Status.CODEFAIL,null);
        }
        return vo;
    }

}
